<!--
 * @Descripttion: 
 * @version: @ 1.0.0
 * @Author: 朱江洲
 * @Date: 2021-12-22 16:48:30
 * @LastEditors: 朱江洲
 * @LastEditTime: 2022-05-07 09:40:24
-->
<template>
  <div class="app-container">
    <div class="header">
      <div class="bread" :class="chooise == true ? 'active' : ''" @click="user">
        <p>用户</p>
      </div>
      <div
        class="bread"
        :class="chooise == false ? 'active' : ''"
        @click="school"
      >
        <p>学校</p>
      </div>
    </div>
    <el-table :data="tableData" :height="tableHight" v-if="chooise">
      <el-table-column label="用户ID" prop="pSerialNumber"> </el-table-column>
      <el-table-column label="用户昵称" prop="pName"> </el-table-column>
      <el-table-column label="手机号码" prop="phone"> </el-table-column>
      <el-table-column label="关联时间" prop="createTime"> </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <span
            @click="relieve(scope.row)"
            style="color: #1267fa; cursor: pointer"
            >解绑</span
          >
        </template>
      </el-table-column>
    </el-table>
    <div v-else>
      <div style="margin-top: 20px">
        <el-button type="primary" size="default" @click="bindingSchool"
          >关联学校</el-button
        >
      </div>
      <el-table :data="tableData" :height="tableHight">
        <el-table-column label="学校ID" prop="sSerialNumber"> </el-table-column>
        <el-table-column label="学校昵称" prop="sName"> </el-table-column>
        <el-table-column label="手机号码" prop="phone"> </el-table-column>
        <el-table-column label="关联时间" prop="createTime"> </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <span
              @click="relieveSchool(scope.row)"
              style="color: #1267fa; cursor: pointer"
              >解绑</span
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 分页器 -->
    <el-pagination
      prev-text="上一页"
      next-text="下一页"
      background
      :page-size="pageSize"
      :page-sizes="[10, 20, 30, 40, 50, 100]"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pageNum"
      layout=" ->,total, sizes, prev, pager, next, jumper "
      :total="total"
    >
    </el-pagination>
    <!-- 关联学校 -->
    <el-dialog
      title="关联学校"
      :visible.sync="relationShow"
      width="30%"
      @close="relationClose"
      :close-on-click-modal="false"
    >
      <el-form
        :model="schoolForm"
        ref="schoolForm"
        :rules="schoolRules"
        label-position="top"
        :inline="false"
        size="normal"
        v-if="relationShow"
      >
        <el-form-item
          label="学校ID"
          style="margin-left: 25px"
          prop="serialNumber"
        >
          <el-input
            v-model="schoolForm.serialNumber"
            style="width: 330px"
            placeholder="请输入学校ID"
          ></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer">
        <el-button @click="relationShow = false">取消</el-button>
        <el-button type="primary" @click="schoolSubmit">保存</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  bindList,
  unRelate,
  spRelateS,
  showSpRelateS,
  spUnRelateS,
} from "@/api/sys";
export default {
  data() {
    return {
      schoolRules: {
        serialNumber: [
          {
            required: true,
            message: "请输入学校ID",
            trigger: "blur",
          },
        ],
      },
      schoolForm: {
        serialNumber: "",
      },
      relationShow: false,
      chooise: true,
      tableHight: window.innerHeight - 300,
      tableData: [],
      pageSize: 10,
      pageNum: 1,
      total: 0,
    };
  },
  created() {
    this.getList();
  },
  mounted() {
    window.onresize = () => {
      return (() => {
        this.tableHight = window.innerHeight - 300;
      })();
    };
  },
  activated() {
    this.tableHight = window.innerHeight - 300;
  },
  methods: {
    // 获取学校列表
    getSchoolList() {
      showSpRelateS({
        pageSize: this.pageSize,
        pageNum: this.pageNum,
      }).then((res) => {
        this.tableData = res.rows;
        this.total = res.total;
      });
    },
    // 关闭绑定学校
    relationClose() {
      this.schoolForm.serialNumber = "";
    },
    // 保存
    schoolSubmit() {
      spRelateS({
        serialNumber: this.schoolForm.serialNumber,
      }).then((res) => {
        this.$message.success(res.msg);
        this.relationShow = false;
      });
    },
    // 绑定学校
    bindingSchool() {
      this.relationShow = true;
    },
    // 重置
    reset() {
      this.pageNum = 1;
      this.tableData = [];
      this.total = 0;
    },
    // 切换用户
    user() {
      this.reset();
      this.chooise = true;
      this.getList();
    },
    // 切换学校
    school() {
      this.reset();
      this.chooise = false;
      this.getSchoolList();
    },
    async relieve(row) {
      const confirmResult = await this.$confirm(
        "此操作将永久解绑该用户, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).catch((err) => err);
      /*如果用户确认打印confirm,如果用户取消显示cancel*/
      if (confirmResult !== "confirm") {
        return this.$message.info("已取消删除!");
      } else {
        unRelate({
          pId: row.pId,
          spId: row.spId,
        }).then((res) => {
          this.$message.success(res.msg);
          this.getList();
        });
      }
      // console.log('确认了删除')
    },
    async relieveSchool(row) {
      const confirmResult = await this.$confirm(
        "此操作将永久解绑该学校, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).catch((err) => err);
      /*如果用户确认打印confirm,如果用户取消显示cancel*/
      if (confirmResult !== "confirm") {
        return this.$message.info("已取消!");
      } else {
        spUnRelateS({
          id: row.id,
        }).then((res) => {
          this.$message.success(res.msg);
          this.getSchoolList();
        });
      }
      // console.log('确认了删除')
    },

    // 获取列表
    getList() {
      bindList({
        pageSize: this.pageSize,
        pageNum: this.pageNum,
      }).then((res) => {
        this.tableData = res.rows;
        this.total = res.total;
      });
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.pageNum = 1;
      this.getList();
    },
    handleCurrentChange(val) {
      this.pageNum = val;
      this.getList();
    },
  },
};
</script>

<style lang="less" scoped>
.header {
  width: 100%;
  height: 74px;
  display: flex;
  align-items: center;
  border-bottom: 1px solid #ececec;
  .active {
    border-bottom: 3px solid #1267fa;
  }
  .bread {
    width: 80px;
    text-align: center;
    height: 100%;
    display: flex;
    align-items: center;
    vertical-align: middle;
    margin-right: 20px;
    img {
      vertical-align: middle;
    }
    p {
      text-align: center;
      width: 100%;
      font-size: 16px;
      font-family: PingFangSC-Medium, PingFang SC;
      font-weight: 500;
      color: #2b2b2b;
    }
  }
}
.el-pagination {
  margin-top: 24px;
  /deep/ .el-pagination__total {
    width: 40px;
    height: 40px;
    line-height: 40px;
    text-align: center;
    margin-right: 25px;
  }
  /deep/ .btn-prev {
    width: 80px;
    height: 40px;
    text-align: center;
  }
  /deep/ .btn-next {
    width: 80px;
    height: 40px;
    text-align: center;
  }
  /deep/ .number {
    width: 40px;
    height: 40px;
    line-height: 40px;
    text-align: center;
  }
  /deep/ .el-input__inner {
    // width: 40px;
    height: 40px;
    line-height: 40px;
    text-align: center;
  }
  /deep/ .more {
    width: 40px;
    height: 40px;
    line-height: 40px;
    text-align: center;
  }
  /deep/ .el-pagination__jump {
    .el-input__inner {
      width: 40px;
      height: 40px;
    }
  }
}
</style>